#include "unpc.h"

int get(struct prodcons *b)
{
    int data;
    pthread_mutex_lock(&b->lock);
    if(b->writepos == b->readpos)
    {
        pthread_cond_wait(&b->notempty, &b->lock);
    }
    data = b->buffer[b->readpos];
    b->readpos++;

    if (b->readpos >= BUFFER_SIZE)
    {
        b->readpos = 0;
    }

    pthread_cond_signal(&b->notfull);
    pthread_mutex_unlock(&b->lock);
    return data;
}

void *consumer(void *data)
{
    int d;

    while(1)
    {
        d = get(&buffer);
        if (d == OVER)
        {
            break;
        }
        printf("%d\n", d);
    }
    return NULL;
}

